package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import Messages.MessageDeleteEmployee;
import Messages.MessageDeleteRecipe;
import Messages.MessageGetAllRecipes;
import Messages.MessageGetAllRecipesReply;
import Messages.MessageShowAllEmployees;
import Messages.MessageShowAllEmployeesReply;
import Messages.MessageShowAllStandardsMenus;
import Messages.MessageShowAllStandardsMenusReply;
import Messages.MessageShowBusinessMenus;
import Messages.MessageShowBusinessMenusReply;

/**
 * Class for creation right side panel for control of selected recipe
 * @author Andrey,Igor
 *
 */
public class PanelFunctionListOfAllRecipes extends PanelFunctionsEmpty{
	
	private Vector<String> allRecipes;
	private MainMenu mainMenu;
	private JButton button_editRecipe;
	private JButton button_createRecipe;
	private JButton button_deleteRecipe;
	private JButton button_ListStandMenu;
	private JButton button_ListBussMenu;
	private JButton button_HomeScreen;
	
	public PanelFunctionListOfAllRecipes(MainMenu mm,Vector <String> recipes)
	{
		super();
		this.allRecipes=recipes;
		this.mainMenu=mm;
		this.add(get_button_editRecipe());
		this.add(get_button_createRecipe());
		this.add(get_button_deleteRecipe());
		this.add(get_button_ListAllStandartMenu());
		this.add(get_button_listAllBusinessMenu());
		this.add(get_button_HomeScreen());
	}
		
	
	
	/**
	 * Method for creation JButton button_editRecipe that open window for updating selected recipe(PfUpdateRecipeNew)
	 * @return button_editRecipe
	 */
	private JButton get_button_editRecipe() 
	{
		
		if (button_editRecipe == null) 
		{			
			button_editRecipe=new JButton("לעדכן מתכון");  			 
			button_editRecipe.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_editRecipe.setPreferredSize(new Dimension(220, 30));
			
			button_editRecipe.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(allRecipes.isEmpty()) {
						JOptionPane.showMessageDialog(null,"יש לבחור מתכון");
			    		 return;
					
					}
					else{
						
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
						mainMenu.getContentPane().add(new PfUpdateRecipeNew(mainMenu,allRecipes), BorderLayout.CENTER);	
						mainMenu.getContentPane().add(new PanelFunctionEmptyWithoutKeyboard(mainMenu), BorderLayout.EAST);
						mainMenu.getContentPane().validate();	
							}
				}
			});
		}
		return button_editRecipe;
	}
	
	/**
	 * Method for creation JButton button_createRecipe that open window for creation new recipe(PfAdd_new_recipe)
	 * @return button_createRecipe
	 */
	private JButton get_button_createRecipe() 
	{
		if (button_createRecipe == null) 
		{			
			button_createRecipe=new JButton("לבנות מתכון");  			 
			button_createRecipe.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_createRecipe.setPreferredSize(new Dimension(220, 30));
			
			button_createRecipe.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
					mainMenu.getContentPane().add(new PfAdd_new_recipe(mainMenu), BorderLayout.CENTER);
					mainMenu.getContentPane().add(new PanelFunctionsManagementMenu(mainMenu), BorderLayout.EAST);
					mainMenu.getContentPane().validate();						
				}
			});
		}
		return button_createRecipe;
	}
	
	private JButton get_button_deleteRecipe() 
	{
		if (button_deleteRecipe == null) 
		{			
			button_deleteRecipe=new JButton("למחוק מתכון");  			 
			button_deleteRecipe.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_deleteRecipe.setPreferredSize(new Dimension(220, 30));
			
			button_deleteRecipe.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(allRecipes.isEmpty()) {
						JOptionPane.showMessageDialog(null,"יש לבחור מתכון");
			    		 return;
					
					}
					else
					{
					int n = JOptionPane.showConfirmDialog(
						    null,
						    "    אתה בטוח?",
						    "    בקשת אישור  ",
						    JOptionPane.YES_NO_OPTION);
					 //Object selectedValue = pane.getValue();
					 if(n==1){
						 
					 }
					 else{
						 
						MessageDeleteRecipe msgDelEmp=new MessageDeleteRecipe (allRecipes.get(0));
						try {
							mainMenu.ms.client.sendToServer(msgDelEmp);
							mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
							MessageGetAllRecipes msgShowRecipes=new MessageGetAllRecipes( );
							mainMenu.ms.client.sendToServer(msgShowRecipes);
							MessageGetAllRecipesReply resultMenu=(MessageGetAllRecipesReply)mainMenu.ms.client.GetMessage();
							mainMenu.getContentPane().add(new PfShow_all_recipes(mainMenu,resultMenu.getRecipes()), BorderLayout.CENTER);	
							mainMenu.getContentPane().validate();
						}
						catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
				}}}
			});
		}
		return button_deleteRecipe;
	}
	
	
	private JButton get_button_ListAllStandartMenu() 
	{
		if (button_ListStandMenu == null) 
		{	final Vector<String> arr = new Vector<String>();		
			button_ListStandMenu=new JButton("רשימת תפריטים רגילים"); 
			if(this.mainMenu.getUser().getPermission()!=3)
				button_ListStandMenu.setEnabled(false);
			button_ListStandMenu.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_ListStandMenu.setPreferredSize(new Dimension(220, 30));
			
			button_ListStandMenu.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					MessageShowAllStandardsMenus msgShowMenu=new MessageShowAllStandardsMenus( );
					
					try {
						mainMenu.ms.client.sendToServer(msgShowMenu);
						MessageShowAllStandardsMenusReply resultMenu=(MessageShowAllStandardsMenusReply)mainMenu.ms.client.GetMessage();
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(new PfSearch_standard_menu(mainMenu,resultMenu.getResultMenu()), BorderLayout.CENTER);	
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
					mainMenu.getContentPane().add(new PanelFunctionMenuAllStandartsMenu(mainMenu,arr), BorderLayout.EAST);
					mainMenu.getContentPane().validate();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return button_ListStandMenu;
	}
	
	private JButton get_button_listAllBusinessMenu() 
	{   final Vector<String> arr = new Vector<String>();
		if (button_ListBussMenu == null) 
		{			
			button_ListBussMenu=new JButton("רשימת תפריטים עסקיים"); 
			if(this.mainMenu.getUser().getPermission()!=3)
				button_ListBussMenu.setEnabled(false);
			button_ListBussMenu.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_ListBussMenu.setPreferredSize(new Dimension(220, 30));
			
			button_ListBussMenu.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					MessageShowBusinessMenus msgShowMenu=new MessageShowBusinessMenus( );
					
					try {
						mainMenu.ms.client.sendToServer(msgShowMenu);
						MessageShowBusinessMenusReply resultMenu=(MessageShowBusinessMenusReply)mainMenu.ms.client.GetMessage();
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
						mainMenu.getContentPane().add(new PfSearch_business_menu(mainMenu,resultMenu.getResultMenu()), BorderLayout.CENTER);	
						mainMenu.getContentPane().add(new PanelFunctionMenuListAllBusMenu(mainMenu,arr), BorderLayout.EAST);
						mainMenu.getContentPane().validate();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.out.println("tables actionPerformed()"); 							
				}
			});
		}
		return button_ListBussMenu;
	}
	
	//-----Return to first screen in this module 
	private JButton get_button_HomeScreen() 
	{
		if (button_HomeScreen == null) 
		{			
			button_HomeScreen=new JButton("תפריט ראשי"); 
			if(this.mainMenu.getUser().getPermission()!=3)
				button_HomeScreen.setEnabled(false);
			button_HomeScreen.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_HomeScreen.setPreferredSize(new Dimension(220, 30));
			
			button_HomeScreen.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
                       MessageShowBusinessMenus msgShowMenu=new MessageShowBusinessMenus( );
					
					try {
						mainMenu.ms.client.sendToServer(msgShowMenu);
						MessageShowBusinessMenusReply resultMenu=(MessageShowBusinessMenusReply)mainMenu.ms.client.GetMessage();
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
						mainMenu.getContentPane().add(new PfSearch_business_menu(mainMenu,resultMenu.getResultMenu()), BorderLayout.CENTER);	
						mainMenu.getContentPane().add(new PanelFunctionsManagementMenu(mainMenu), BorderLayout.EAST);
						mainMenu.getContentPane().validate();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.out.println("tables actionPerformed()"); 							
				}
			});
		}
		return button_HomeScreen;
	}
}
